walletSweeper.catch   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 1
1
var blocktrail = require('../');
2
3
var backupDataV3 = {
4
    walletVersion:                   3,
5
    encryptedPrimaryMnemonic:        "library fish steak unfair series jacket enhance unique witness session abandon ability hole spread black stuff gun country icon hair sugar mixture rib mansion neglect afraid unlock barrel today misery shift replace unusual ticket zone habit aspect globe glad find space tape remove priority describe smart annual sign direct regular can pear huge rather wish travel stomach mobile situate stand",
6
    backupMnemonic:                  "snap lyrics december view youth dynamic physical shed certain govern cigar top submit measure minute flight used glass tragic basket alarm scorpion wagon oblige",
7
8
    passwordEncryptedSecretMnemonic: "library faith derive beach blast sustain index fold actor session abandon access forest around canal theme body denial excuse believe voyage anchor state meadow assist ostrich trick lock near uniform suspect person autumn dentist rent square idle motion calm time focus help legal subject quality pupil atom weather start kite enable today primary rail flag clarify clarify syrup fee clump",
9
    password:                        "roobsieroobs",
10
11
    blocktrailKeys: [
12
        {
13
            keyIndex: 0,
14
            path:     "M/0'",
15
            pubkey:   'tpubD8UrAbbGkiJUnPP85sYJZ6ozSsgfk4qH9jbzWFMUGhfsgKPEzLLpNvgkFm9P4ktkAbPpX1ACns2PdfBT8ZF9vFjaU5GKQCZ892AJSJ2VgDK'
16
        },
17
        {
18
            keyIndex: 9999,
19
            path:     "M/9999'",
20
            pubkey:   'tpubD9q6vq9zdP3gbhpjs7n2TRvT7h4PeBhxg1Kv9jEc1XAss7429VenxvQTsJaZhzTk54gnsHRpgeeNMbm1QTag4Wf1QpQ3gy221GDuUCxgfeZ'
21
        }
22
    ]
23
};
24
25
var useTestnet = true;
26
27
28
// we need a bitcoin data service to find utxos. We'll use the BlocktraiBitcoinService, which in turn uses the Blocktrail SDK
29
var bitcoinDataClient = new blocktrail.BlocktrailBitcoinService({
30
    apiKey:     "MY_APIKEY",
31
    apiSecret:  "MY_APISECRET",
32
    network:    "BTC",
33
    testnet:    useTestnet
34
});
35
36
var discoverAndSweep = true;         //do we want to discover funds and sweep them to another wallet at the same time?
37
var recoverWithPassword = true;     //do we want to try and recover with or without the password?
38
39
40
/**
41
 * create an instance of the wallet sweeper, which generates the wallet keys from the backup data
42
 *
43
 */
44
var sweeperOptions = {
45
    network: 'btc',
46
    testnet: useTestnet,
47
    logging: true,          // display extra info in console
48
    sweepBatchSize: 10      // number of addresses to check at a time (use a larger number for older wallets)
49
};
50
var walletSweeper;
51
if (recoverWithPassword) {
52
    console.log('Creating wallet keys using password method...');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
53
    walletSweeper = new blocktrail.WalletSweeper(backupDataV3, bitcoinDataClient, sweeperOptions);
54
} else {
55
    /**
56
     * if the wallet password is forgotten for a V2 wallet, it is possible to use the "Encrypted Recovery Secret" on the backup pdf
57
     * along with a decryption key which must be obtained directly from Blocktrail.
58
     */
59
    backupDataV3.password = null;
60
    backupDataV3.encryptedRecoverySecretMnemonic = "library faint leopard present project pair census prison aisle session abandon achieve clay abandon light brave olympic profit liquid fan ribbon twist glance hold file wrestle unhappy wreck unveil shrug round record jump seven galaxy skate cattle hedgehog humble purity hair hand digital mixture else senior witness great art seat coach trend transfer negative general fruit bright order fresh festival";
61
    backupDataV3.recoverySecretDecryptionKey = new Buffer("9e2eedf5716b0b1620fb8d2817d4760ef68419ea1ce6aca71845c71fe23e68f9", 'hex');
0 ignored issues
show
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
62
63
    console.log('Creating wallet keys using encrypted secret method...');
64
    walletSweeper = new blocktrail.WalletSweeper(backupDataV3, bitcoinDataClient, sweeperOptions);
65
}
66
67
68
/**
69
 * now we can discover funds in the wallet, and then create a transaction to send them all to a new address
70
 *
71
 */
72
if (!discoverAndSweep) {
73
    //Do wallet fund discovery - can be run separately from sweeping
74
    console.log('-----Discovering Funds-----');
75
    var batchSize = 25;
76
    walletSweeper.discoverWalletFunds(batchSize)
77
        .progress(function(progress) {
78
            console.info(progress);
79
        })
80
        .then(function(result) {
81
            console.log(result);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
82
        })
83
        .catch(function(err) {
84
            console.error(err);
85
        });
86
87
} else {
88
    // Do wallet fund discovery and sweeping - if successful you will be returned a signed transaction ready to submit to the network
89
    console.log('\n-----Sweeping Wallet-----');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
90
    var receivingAddress = "2NFLXEc5m1X2Z8NB5QTVd9KJtN8bJBqz1Xp";
91
    walletSweeper.sweepWallet(receivingAddress)
92
        .progress(function(progress) {
93
            console.info(progress);
94
        })
95
        .then(function(result) {
96
            console.log(result);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
97
        })
98
        .catch(function(err) {
99
            console.error(err);
100
        });
101
102
}
103